﻿using HRX.PayCenter.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRX.PayCenter.Web.Extensions
{
    /// <summary>
    /// 
    /// </summary>
    public static class HttpRequestExtension
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bodyStream"></param>
        /// <returns></returns>
        public static IDictionary<string, string> BodyStreamDict(this Stream bodyStream)
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            if (bodyStream == null)
            {
                return dict;
            }
            if (bodyStream.CanSeek)
            {
                bodyStream.Position = NumConst.Zero;
            }
            using (StreamReader reader = new StreamReader(bodyStream))
            {
                string content = reader.ReadToEnd();
                if (!string.IsNullOrWhiteSpace(content))
                {
                    var contentObj = NewtonsoftJsonHelper.DeserializeObject(content) as JObject;
                    if (contentObj != null)
                    {
                        dict = new Dictionary<string, string>();
                        foreach (var kv in contentObj)
                        {
                            dict.Add(kv.Key, kv.Value.ToString());
                        }
                    }
                }
            }
            return dict;
        }

        /// <summary>
        /// GET字典
        /// </summary>
        /// <param name="queryCollection"></param>
        /// <returns></returns>
        public static IDictionary<string, string> QueryDict(this IQueryCollection queryCollection)
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            foreach (var query in queryCollection)
            {
                dict.Add(query.Key, query.Value);
            }

            return dict;
        }

        /// <summary>
        /// 查询字符串
        /// </summary>
        /// <param name="queryCollection"></param>
        /// <returns></returns>
        public static string QueryStr(this IQueryCollection queryCollection)
        {
            if (queryCollection == null)
            {
                return "-";
            }
            return queryCollection.ToStringFromDict();
        }

        /// <summary>
        /// POST字典
        /// </summary>
        /// <param name="formCollection"></param>
        /// <returns></returns>
        public static IDictionary<string, string> FormDict(this IFormCollection formCollection)
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            foreach (var form in formCollection)
            {
                dict.Add(form.Key, form.Value);
            }

            return dict;
        }

        /// <summary>
        /// Post字符串
        /// </summary>
        /// <param name="formCollection"></param>
        /// <returns></returns>
        public static string FormStr(this IFormCollection formCollection)
        {
            if (formCollection == null)
            {
                return "-";
            }
            return formCollection.ToStringFromDict();
        }
    }
}
